package com.hpdz.hotel_back.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hpdz.hotel_back.entity.Admin;
import com.hpdz.hotel_back.entity.Employee;
import com.hpdz.hotel_back.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 *    todo: admin 接口实现完毕! 未对接前端,需要测试!
 *
 */



@Slf4j
@RestController
public class AdminController {
    @Autowired
    AdminService adminService;


    /** 接口 0: admin 注册!
     * name: /admin_register
     *
     * method: post;
     *
     * parameter: admin;
     *
     * return true/false;
     */
    @PostMapping("/admin_register")
    public Boolean register(@RequestBody Admin admin){
        return adminService.register(admin);
    }
    /**
     *  接口 1: 登录需求!
     *  name: /admin_login
     *
     *  method: post;
     *
     *  parameter:  admin;
     *
     *  return true ? admin : null;
     */
    @PostMapping("/admin_login")
    public Admin login(@RequestBody Admin admin, HttpServletRequest request){
        log.debug("admin_login",admin);
        Admin login = adminService.login(admin);
        if(login != null){
            HttpSession session = request.getSession(true);
            session.setAttribute("cur_admin",login);
            login.setPassword("");
            return login;
        }else {
            return null;
        }
    }





    /**
     * 接口 2: 分页获取所有员工列表
     * name: /get_all_emp
     *
     * method: get;
     *
     * parameter: current,size
     *
     * return true ? List<Employee> : null;
     *
     */
    @GetMapping("/get_all_emp/{current}/{size}")
    public IPage<Employee> getAll(@PathVariable Integer current,@PathVariable Integer size){
        return adminService.getAllEmp(current,size);
    }


    /**
     * 接口 3: 修改员工信息
     *  name: /modify_emp
     *
     *  method: post;
     *
     *  parameter: emp
     *
     *  return true / false
     */
    @PostMapping("/modify_emp")
    public boolean modifyEmployee(@RequestBody Employee emp){
        return adminService.modifyEmployee(emp);
    }


    /**
     * 接口 4: 添加员工
     *  name: /add_emp
     *
     *  method: post;
     *
     *  parameter: emp;
     *
     *  return true/false;
     */
    @PostMapping("/add_emp")
    public boolean addEmp(@RequestBody Employee emp){
        return adminService.addEmp(emp);
    }


    /**
     *  接口 5: 修改管理员个人信息
     *  name: /modify_adm
     *
     *  method: post;
     *
     *  parameter: admin;
     *
     *  return true/false;
     */
    @PostMapping("/modify_adm")
    public boolean modifyAdmin(@RequestBody Admin admin){
        return adminService.modifyAdmin(admin);
    }

    /**
     * 接口 6: 通过员工的nick_name分页查找员工
     * name: /search_emp
     *
     * method: post;
     *
     * parameter: current,size,emp
     *
     * return true ? list : null;
     */
    @GetMapping ("/search_emp/{current}/{size}")
    public IPage<Employee> searchEmp(@PathVariable Integer current, @PathVariable Integer size, Employee emp){
        return adminService.searchEmpPage(current,size,emp);
    }

    /**
     *  接口 7: 通过id删除员工
     *  name: delete_emp
     *
     *  method: get;
     *
     *  parameter: id;
     *
     *  return true/false;
     *
     */
    @GetMapping("/delete_emp/{id}")
    public boolean deleteEmp(@PathVariable Integer id){
        return adminService.deleteEmp(id);
    }



}
